package com.ruoyi.web.controller.app;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.ChannelEnum;
import com.ruoyi.common.exception.BusiException;
import com.ruoyi.common.utils.GoogleAuthenticationTool;
import com.ruoyi.common.utils.QRCodeUtil;
import com.ruoyi.system.domain.Bank;
import com.ruoyi.system.dto.BankAddDto;
import com.ruoyi.system.service.BankService;
import com.ruoyi.system.service.UserRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.InputStream;
import java.util.List;

@Api(tags = "用户端:银行卡相关接口")
@RestController
@RequestMapping("/app/bank")
public class BankController extends AppController {
    @Resource
    private BankService bankService;
    @Resource
    private UserRateService userRateService;


    @Log(title = "开启/关闭", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "新增或修改,有id为修改")
    @PostMapping("updateStatus")
    public ResponseResult updateStatus(@RequestBody BankAddDto bankAddDto) {
        Bank bank = new Bank();
        bank.setStatus(bankAddDto.getStatus());
        bank.setId(bankAddDto.getId());
        this.bankService.updateDomain(bank);
        return this.setSuccessResult();
    }


    @Log(title = "收款方式管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "新增或修改,有id为修改")
    @PostMapping("save")
    public ResponseResult save(@Valid @RequestBody BankAddDto bankAddDto) {
        SysUser user = getSysUser();
        Bank bank = BeanUtil.copyProperties(bankAddDto, Bank.class);
        GoogleAuthenticationTool.checkGoogleCode(user.getRandomSecretKey(), bankAddDto.getCode());
        if (bank.getId() != null) {
            return update(bank);
        }

        Long userId = user.getUserId();

        bank.setUserId(userId);
        bank.setUsername(user.getUserName());

        this.setBank(bank);

        this.bankService.addDomain(bank);
        return this.setSuccessResult();
    }


    private void setBank(Bank bank) {
        userRateService.checkUserRate(bank.getUserId(), bank.getChannelCode());
        bank.setCardNo(StrUtil.trim(bank.getCardNo()));
        String channelCode = bank.getChannelCode();
        bank.setChannelName(ChannelEnum.getName(channelCode));
        if (StrUtil.equals(bank.getChannelCode(), ChannelEnum.TB.getCode())) {//如果是淘宝
            if (StrUtil.isBlank(bank.getCardNo()) || !StrUtil.contains(bank.getCardNo(), "http")) {
                throw new BusiException("请添加正确的商品连接");
            }
            if (bank.getMaxAmount() == null) {
                throw new BusiException("请输入限制金额");
            }
            String index = "http";
            String result = index + StrUtil.subAfter(bank.getCardNo(), index, true);
            String url = null;
            String[] split = result.split(" ");
            for (int i = 0; i < split.length; i++) {
                url = split[i];
                if (StrUtil.isNotBlank(url) &&
                        (StrUtil.contains(url, "https:")
                                || StrUtil.contains(url, "http:"))) {

                    bank.setCardNo(url);
                    break;
                }
            }


        }


        if (StrUtil.isNotBlank(bank.getZfbUserId())) {
            String replace = StrUtil.replace(bank.getZfbUserId(), " ", "");

            bank.setZfbUserId(replace);
        }
//        try {
//            //支付宝转账  先暂时不需要
//            if (StrUtil.contains(bank.getChannelCode(), ChannelEnum.ZFB.getCode())) {
//
//                if (StrUtil.isBlank(bank.getBankName())) {
//                    throw new BusiException("请上传支付宝二维码");
//                }
//                InputStream inputStreamFromUrl = QRCodeUtil.getInputStreamFromUrl(bank.getBankName());
//                //   InputStream inputStreamFromUrl = QRCodeUtil.getInputStreamFromUrl(bank.getBankName());
//                String read = QRCodeUtil.read(inputStreamFromUrl);
//                if (StrUtil.isBlank(read)) {
//                    throw new BusiException("请上传正确的支付宝二维码");
//                }
//                bank.setZfbUrl(read);
//            }
//        } catch (Exception e) {
//            throw new BusiException("操作失败！");
//        }


    }


    public ResponseResult update(@RequestBody Bank bank) {
        Long userId = this.getUserId();
        bank.setUserId(userId);
        this.setBank(bank);
        this.bankService.updateDomain(bank);
        return this.setSuccessResult();
    }

    @Log(title = "收款方式管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除")
    @PostMapping("del")
    public ResponseResult del(Long id) {
        //Long userId = this.getUserId();
        this.bankService.delById(id);
        return this.setSuccessResult();
    }

//    @ApiOperation(value = "列表")
//    @PostMapping("list")
//    public ResponseResult<List<Bank>> list(@RequestBody Bank bank) {
//        this.startPage();
//        Long userId = this.getUserId();
//        bank.setUserId(userId);
//        List<Bank> list = bankService.findList(bank);
//        return this.setSuccessResult(list);
//    }

    @ApiOperation(value = "列表")
    @GetMapping("bankList")
    public ResponseResult<List<Bank>> bankList(Bank bank) {
        this.startPage();
        Long userId = this.getUserId();
        if (StrUtil.isBlank(bank.getType())) {
            bank.setType(null);
        }
        bank.setUserId(userId);
        List<Bank> list = bankService.findList(bank);
//        return this.setSuccessResult(this.getPageResult(list));
        return this.setSuccessResult(list);
    }

    public static void main(String[] args) {
//        String s1 = StrUtil.subBefore("www.baidu.com/adfadf/1.jpg?sign", "?", true);
////        String s2 = StrUtil.subBefore("www.baidu.com/adfadf/1.jpg?sign", "?", false);
//        String s2 = StrUtil.subBefore("www.baidu.com/adfadf/1.jpg", "?", true);
        String s = "【闲鱼】https://m.tb.cn/h.5rs9T0O?tk=hErwW8lqeKE CZ0000 「我在闲鱼发布了【一张纸】」点击链接直接打开";
//        System.out.println(s1);
//        System.out.println(s2);
        String index = "http";
        String result = index + StrUtil.subAfter(s, index, true);
        System.out.println(result);

    }


}
